//
//  LoginViewController.m
//  Snigger
//
//  Created by Young on 16/8/18.
//  Copyright © 2016年 Smile. All rights reserved.
//

#import "LoginViewController.h"

#import "OtherLoginsViewController.h"
#import "RegisterFirstViewController.h"
#import "Alertor.h"
#import "EMClient.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *looksImageView;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *otherLoginWaysButton;
@property (weak, nonatomic) IBOutlet UIButton *returnButton;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed:236 / 256.0 green:237 / 256.0 blue:240 / 256.0 alpha:1.0];
    _looksImageView.layer.cornerRadius = _looksImageView.bounds.size.width / 2.0;
    _looksImageView.layer.masksToBounds = YES;
    
    _loginButton.layer.cornerRadius = 5;
    _loginButton.layer.masksToBounds = YES;
    _otherLoginWaysButton.layer.cornerRadius = 5;
}

#pragma mark - 登录
- (IBAction)loginButtonClicked:(id)sender {
    NSString *userName = _userNameTextField.text;
    NSString *password = _passwordTextField.text;
    [[EMClient sharedClient] asyncLoginWithUsername:userName password:password success:^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录成功" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *successAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [[EMClient sharedClient].options setIsAutoLogin:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alertController addAction:successAction];
        [self presentViewController:alertController animated:YES completion:nil];

    } failure:^(EMError *aError) {
        [self presentViewController:[[Alertor sharedAlertor] alertWithTitle:@"登录失败" message:[aError errorDescription] actionTitle:@"确定"] animated:YES completion:nil];
    }];
}

- (IBAction)otherWaysToLoginClicked:(id)sender {
    OtherLoginsViewController *otherLoginsVC = [[OtherLoginsViewController alloc] init];
    [self.navigationController pushViewController:otherLoginsVC animated:YES];
}

#pragma mark - 注册
- (IBAction)registClicked:(id)sender {
    RegisterFirstViewController *registerFirst = [[RegisterFirstViewController alloc] init];
    [self.navigationController pushViewController:registerFirst animated:YES];
}
#pragma mark - 忘记密码
- (IBAction)somethingWrongClicked:(id)sender {
    
}

#pragma mark - 返回
- (IBAction)returnButtonClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
